import lunar from './lunar'
class Calendar {
    constructor({
        // eslint-disable-next-line no-unused-vars
        date,
        range
    } = {}) {
        // 开始时间
        this.date = this.getDate(date)
        // 时间范围
        this.range = range
        // 每月日期
        this.month = {}
        // 一周时间
        this.week = {}
        // 一天的时间
        this.hours = {}
    }
	/**
	 * 设置日期
	 * @param {Object} date
	 */
	setDate(date) {
		this.selectDate = this.getDate(date)
		this.getMonth(this.selectDate.fullDate)
        this.getHours()
        this.getWeek(this.selectDate.fullDate)
	}
    /**
     * 需要创建时间, yyyy-MM-DD HH:mm:ss
     */
    getDate(date, AddDayCount = 0, str = 'day') {
        if (!date) {
			date = new Date()
		}
		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/')
		}
		const dd = new Date(date)
		switch (str) {
			case 'day':
				dd.setDate(dd.getDate() + AddDayCount) // 获取AddDayCount天后的日期
				break
			case 'month':
				if (dd.getDate() === 31) {
					dd.setDate(dd.getDate() + AddDayCount)
				} else {
					dd.setMonth(dd.getMonth() + AddDayCount) // 获取AddDayCount天后的日期
				}
				break
            case 'year':
                dd.setFullYear(dd.getFullYear() + AddDayCount) // 获取AddDayCount天后的日期
                break
            case 'week':

                break
		}
		const y = dd.getFullYear()
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1 // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate(): dd.getDate() // 获取当前几号，不足10补0
		return {
			fullDate: y + '-' + m + '-' + d,
			year: y,
			month: m,
			date: d,
			day: dd.getDay()
		}
    }
    /**
     * 获取上月剩余天数
     */
    getLastMonthDays(firstDay, full) {
        let dateArr = []
		for (let i = firstDay; i > 0; i--) {
            const current = new Date(full.year, full.month - 1, -i + 1)
			const beforeDate = current.getDate()
            const weekend = current.getDay() < 1 || current.getDay() > 5
			dateArr.push({
				date: beforeDate,
				month: full.month - 1,
				lunar: this.getlunar(full.year, full.month - 1, beforeDate),
				disable: true,
                weekend,
                fullDate: this.getDate(current).fullDate
			})
		}
		return dateArr
    }
    /**
     * 获取本月剩余天数
     */
    getCurrentMonthDays(dateData, full) {
        let dateArr = []
		let fullDate = this.date.fullDate
		for (let i = 1; i <= dateData; i++) {
			// let isinfo = false
			let nowDate = full.year + '-' + (full.month < 10 ?
				full.month : full.month) + '-' + (i < 10 ?
				'0' + i : i)
			// 是否今天
            const nowFormatDate = new Date(nowDate)
			let isDay = fullDate === nowDate
            let weekend = nowFormatDate.getDay() < 1 || nowFormatDate.getDay() > 5
			let data = {
				fullDate: nowDate,
				year: full.year,
				date: i,
				month: full.month,
				lunar: this.getlunar(full.year, full.month, i),
				disable: false,
				isDay,
                weekend
			}
			dateArr.push(data)
		}
		return dateArr
    }
    getNextMonth(surplus, full) {
        let dateArr = []
		for (let i = 1; i < surplus + 1; i++) {
            const current = new Date(full.year, full.month, i)
            const weekend = current.getDay() < 1 || current.getDay() > 5
			dateArr.push({
				date: i,
				month: Number(full.month) + 1,
				lunar: this.getlunar(full.year, Number(full.month) + 1, i),
				disable: true,
                weekend,
                fullDate: this.getDate(current).fullDate
			})
		}
		return dateArr
    }
    /**
     * 获取当前日期详情
     */
    getInfo(date) {
        if (!date) {
			date = new Date()
		}
		const dateInfo = this.canlender.find(item => item.fullDate === this.getDate(date).fullDate)
        if(dateInfo && dateInfo.date < 10){
            dateInfo.date = '0' + dateInfo.date
        }
		return dateInfo
    }
    /**
     * 比较时间是否相等
     */
    dateEqual(before, after) {
        // 计算截止时间
		before = new Date(before.replace('-', '/').replace('-', '/'))
		// 计算详细项的截止时间
		after = new Date(after.replace('-', '/').replace('-', '/'))
		if (before.getTime() - after.getTime() === 0) {
			return true
		} else {
			return false
		}
    }
    /**
	 * 计算阴历日期显示
	 */
	getlunar(year, month, date) {
		return lunar.solar2lunar(year, month, date)
	}
    /**
     * 获取每月数据
     */
    getMonth(dateData) {
        const {
            canlender,
            weeks
        } = this.getStartMonth(dateData)
		this.canlender = canlender
		this.month = weeks
    }
    getStartMonth(dateData) {
        const {
            year,
            month
        } = this.getDate(dateData)
        let firstDay = new Date(year, month - 1, 0).getDay()
		let currentDay = new Date(year, month, 0).getDate()
		let dates = {
			lastMonthDays: this.getLastMonthDays(firstDay, this.getDate(dateData)), // 上个月末尾几天
			currentMonthDys: this.getCurrentMonthDays(currentDay, this.getDate(dateData)), // 本月天数
			nextMonthDays: [], // 下个月开始几天
			weeks: []
		}
		let canlender = []
		const surplus = 42 - (dates.lastMonthDays.length + dates.currentMonthDys.length)
		dates.nextMonthDays = this.getNextMonth(surplus, this.getDate(dateData))
		canlender = canlender.concat(dates.lastMonthDays, dates.currentMonthDys, dates.nextMonthDays)
		let weeks = {}
		// 拼接数组  上个月开始几天 + 本月天数+ 下个月开始几天
		for (let i = 0; i < canlender.length; i++) {
			if (i % 7 === 0) {
				weeks[parseInt(i / 7)] = new Array(7)
			}
			weeks[parseInt(i / 7)][i % 7] = canlender[i]
		}
        return {
            canlender,
            weeks
        }
    }
    /**
     * 获取一天的小时
     */
    getHours() {
        let start = 0 // 开始
        let end = 24 // 结束
        if(this.range.length > 0) {
            start = this.range[0]
            end = this.range[1]
        }
        if(start >= end) return this.hours = []
        let hours =  []
        for(let i = start; i < end; i ++) {
            if(i < 10) {
                i = '0' + i
            }
            hours.push(i + ':00')
        }
        this.hours = hours
    }
    /**
     * 获取一周的时间
     */
    getWeek(dateData) {
        this.week = this.getStartWeek(dateData)
    }
    /**
     * 获取限定时间, 按周一开始算
     */
    getStartWeek(dateData, weekDay = 7) {
        const {
            year,
            month,
            date,
            day
        } = this.getDate(dateData)
        // 获取本周的周一是哪一天, 
        // 本周的周日是哪一天
        // 不管今天是哪一天: 周一, 周日都是固定的
        const current = new Date(year + '-' + month + '-' + date).getTime()
        const nowDate = this.getDate(new Date())
        const now = nowDate.year + '-' + nowDate.month + '-' + nowDate.date
        let firstDay = new Date(current - (day - 1) * 24 * 60 * 60 * 1000)
        if(day == 0) {
            firstDay = new Date(current - 6 * 24 * 60 * 60 * 1000)
        }
        firstDay = firstDay.getTime()
        let week = []
        for(let i = 0; i < weekDay; i ++) {
            const temp = new Date(firstDay + i * 24 * 60 * 60 * 1000)
            let date = this.getDate(temp)
            const tempDate = date.year + '-' + date.month + '-' + date.date
            if(now === tempDate){
                date.isDay = true
            }else{
                date.isDay = false
            }
            if(date.day == 0 || date.day == 6) {
                date.weekend = true
            }else{
                date.weekend = false
            }
            date.lunar = this.getlunar(date.year, date.month, date.date)
            week.push(date)
        }
        return week
    }
    /**
     * 获取指定多少周的时间
     * @dateData 开始时间
     * @weekNum 多少周
     * @currentPosition 当前周在第几周 从0开始
     */
    getAnyWeek(dateData, weekNum, currentPosition) {
        let arr = []
        const { year, month, date, day } = this.getDate(dateData)
        const current = new Date(year + '-' + month + '-' + date).getTime()
        const firstWeekDay = current - currentPosition * 7 * 24 * 60 * 60 * 1000 // 开始时间
        // const lastWeekDay = firstWeekDay + (weekNum - 1) * 7 * 24 * 60 * 60 * 1000 // 结束时间
        for(let i = 0; i < weekNum; i ++){
            const week = this.getStartWeek(new Date(firstWeekDay + i * 7 * 24 * 60 * 60 * 1000))
            arr.push(week)
        }
        return arr
    }
}
export default Calendar